﻿using System;
using System.Globalization;
using System.IO;
using IdSharp.AudioInfo;

namespace GDFlacTool.Common
{
    [Serializable]
    public class TrackShn : Track
    {
        #region Fields.

        private readonly bool _isEmpty;
        private readonly string _trackFormat = string.Empty;
        private readonly bool _isSupportedFormat;
        private IAudioFile _audioFile;

        #endregion

        #region Constructors.

        public TrackShn() { }

        public TrackShn(string path)
        {
            base.FullPath = path;

            _trackFormat = "shn";

            _isSupportedFormat = false;

            if (CreateShnFile(FullPath))
            {
                ReadFileInfo();

                _isEmpty = false;
            }
            else
            {
                FullPath = string.Empty;

                _isEmpty = true;
            }
        }

        #endregion

        #region Override Methods.

        public override bool IsTagCorrupt(out string message)
        {
            message = "Tag is not supported in shn files";

            return false;
        }

        public override string TrackFormat
        {
            get { return _trackFormat; }
        }

        public override bool IsSupportedTag
        {
            get { return _isSupportedFormat; }
        }

        public override bool IsEmpty
        {
            get { return _isEmpty; }
        }

        public override bool RenameTrack(MaskHelperCollection masks, TrackRename trackRename)
        {
            return false;
        }

        #endregion

        #region Private Methods.

        private bool CreateShnFile(string path)
        {
            try
            {
                _audioFile = AudioFile.Create(path, false);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void ReadFileInfo()
        {
            //_xiphComment = (XiphComment)_taglibfile.GetTag(TagTypes.Xiph);

            var fileInfo = new FileInfo(base.FullPath);

            //Create a file path
            FilePath = string.Format("{0}{1}", Path.GetDirectoryName(FullPath), Path.DirectorySeparatorChar);

            //Read the file name
            FileName = Path.GetFileName(FullPath);
            
            //Read bitrate
            BitRate = Convert.ToInt32(Math.Round(_audioFile.Bitrate, 0)).ToString(CultureInfo.InvariantCulture);

            //Read duration
            TimeSpan ts = TimeSpan.FromSeconds((double)_audioFile.TotalSeconds);

            if (ts.Hours > 0)
            {
                Time = string.Format("{0:0#}:{1:0#}:{2:0#}", ts.Hours, ts.Minutes, ts.Seconds);
            }
            else
            {
                Time = string.Format("{0:0#}:{1:0#}:{2:0#}", ts.Hours, ts.Minutes, ts.Seconds);
            }

            Time = string.Format("{0:0#}:{1:0#}", ts.Minutes, ts.Seconds);

            //Read the sample rate
            SampleRate = _audioFile.Frequency.ToString(CultureInfo.InvariantCulture);

            //Read the file size
            Size = fileInfo.Length;
            
            //Read the bps
            BitsPerSample = "0";

            //Read channels
            Channels = _audioFile.Channels;

            //Read the encoder info
            base.Encoder = _audioFile.FileType.ToString();
        }

        #endregion
    }
}
